<?php

namespace App\Http\Resources\nursery;


use App\Models\nursery\Major;
use Illuminate\Http\Resources\Json\ResourceCollection;

class MajorCollection extends ResourceCollection
{

    protected $user_ids;

    public function __construct($resource,$user_ids)
    {
        parent::__construct($resource);
        $this->user_ids = $user_ids;
        $this->resource = $this->collectResource($resource);
    }


    /**
     * Transform the resource collection into an array.
     *
     * @return \Illuminate\Support\Collection
     */
    public function toArray($request)
    {

        return $this->collection->map(function (Major $item) {
            return [
                'id' => $item->id,
                'img' => $item->img,
                'name' => $item->name??'',
                'num' => $item->majors_sum,
                'follow_num' => getFollowMajorsSum($item->id,$this->user_ids)
            ];
        });
    }
}
